//
//  TTJobChosedViewController.m
//  togetherPlay
//
//  Created by Mac on 2018/12/21.
//  Copyright © 2018 stoneobs.qq.com. All rights reserved.
//

#import "THSubCateViewController.h"
#import "THCategoryControl.h"
#import "THCagetoryModel.h"
#import "THCategoryDataController.h"
@interface THSubCateViewController ()<UITextFieldDelegate>
@property(nonatomic, strong) NSMutableArray                    *dataSouce;/**<  */
@property(nonatomic, strong) NSMutableArray                     *controlArray;/**<  */
@property(nonatomic, strong) THCateModel                     *chosedModel;/**< 选中的第一级模型 */
@property(nonatomic, strong) NSMutableArray                     *goldArray;/**<  */

@property(nonatomic, strong) STButton                     *messageButton;/**<  */
@property(nonatomic, strong) STButton                     *button;/**<  */

@property(nonatomic, strong) STTools_TagMenuView                     *menuView;/**<  */
@end

@implementation THSubCateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.name;
    [self configSubView];
    [self configMenuView];

    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)reloadData{
    [self configSubView];
}
- (void)configMenuView{
    CGFloat buttonWith = UISCREEN_WIDTH/7;
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TMUtils.stoneobs_fun_get_navgationBarBootom, buttonWith, TMUtils.stoneobs_fun_get_tabBarTop -TMUtils.stoneobs_fun_get_navgationBarBootom)];
    scrollView.backgroundColor = WH_UICOLOR_FROM_RGBA(0xF6F6F6);
    [self.view addSubview:scrollView];
    NSArray * array = [self.model.subs stoneobs_fun_arrayFromObjKeyName:@"obj.name"];
    self.controlArray = [NSMutableArray new];
    __weak typeof(self) weakSelf =  self;
    for (NSInteger i = 0; i < array.count; i ++) {
        NSString * title = array[i];
        THCategoryControl * control = [[THCategoryControl alloc] initWithFrame:CGRectMake(0, 0, buttonWith, 55) title:title];
        [scrollView addSubview:control];
        [control setOnSelectedControl:^(THCategoryControl *sender) {
            for (THCategoryControl * existControl in self.controlArray) {
                existControl.selected = NO;
                sender.selected = YES;
                
            }
            THCateModel * model = self.model.subs[i];
            weakSelf.chosedModel = model;
            [weakSelf configTableHeaderView];
        }];
        control.top = i * control.height;
        [self.controlArray addObject:control];
        if (i == 0) {
            control.selected = YES;
            THCateModel * model = weakSelf.dataSouce.firstObject;
            weakSelf.chosedModel = model;
            [weakSelf configTableHeaderView];
        }
        scrollView.contentSize = CGSizeMake(0, control.bottom);
    }
    
}

- (void)configSubView{
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake( UISCREEN_WIDTH/7, TMUtils.stoneobs_fun_get_navgationBarBootom, UISCREEN_WIDTH - UISCREEN_WIDTH/7, TMUtils.stoneobs_fun_get_tabBarTop + 49 - TMUtils.stoneobs_fun_get_navgationBarBootom);
    self.tableView.showsVerticalScrollIndicator = NO;
  
}

- (void)configTableHeaderView{
    __weak typeof(self) weakSelf =  self;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.width, 100)];
    self.tableView.backgroundColor = UIColor.whiteColor;
    CGFloat top = 10;
    
    CGFloat buttonWith = self.tableView.width /3;

    

    for (NSInteger i = 0 ;i < self.chosedModel.subs.count;i ++) {
        THCateModel * subModel = self.chosedModel.subs[i];
        STTools_MenuControl * menuControl = [[STTools_MenuControl alloc] initWithFrame:CGRectMake(0, 0, buttonWith, 130) imageName:@"" title:subModel.name titleColor:STONEOBS_UI_FIRST_TEXT_COLOR imageHeight:80 lableHeight:40];
        [menuControl setOnSelctedControl:^(STTools_MenuControl *control) {
            
            THCateDetailViewController * vc = THCateDetailViewController.new;
            vc.c_id = subModel.c_id;
            [weakSelf pushViewController:vc animated:1];
        }];
        menuControl.top = top + menuControl.height * (i/3);
        menuControl.left = buttonWith * (i%3);
        header.height = menuControl.bottom;
        [menuControl.imageButton sd_setImageWithURL:subModel.icon.mj_url forState:UIControlStateNormal];;
        [header addSubview:menuControl];
    }

    self.tableView.tableHeaderView = header;
    

}


@end
